//
//  CZWelcomeViewController.m
//  小和微博
//
//  Created by 董晓合 on 15/2/26.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZWelcomeViewController.h"
#import "CZtabBarController.h"

@interface CZWelcomeViewController ()
//头像
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
//头像Y
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconConsY;
//文字
@property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;

@end

@implementation CZWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    1.设置圆角
    self.iconImageView.layer.cornerRadius = 50;
    self.iconImageView.layer.masksToBounds = YES;
    
//    2.加载头像
    CZAccount *account = [CZAccount accountFromSandbox];
    NSURL *iconURL = [NSURL URLWithString:account.profile_image_url];
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    [manager downloadImageWithURL:iconURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        if (finished) {
            self.iconImageView.image = image;
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 1.让头像做动画, 从下往上移动
    [UIView animateWithDuration:1 animations:^{
        self.iconConsY.constant += 150;
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            // 2.让文字淡出
            self.welcomeLabel.alpha = 1.0;
        } completion:^(BOOL finished) {
            // 3.自动切换到tabbarcontroller
            // 1.创建Tabbarcontroller
            CZtabBarController *tabbarVc = [[CZtabBarController alloc] init];
            // 2.切换根控制器
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            window.rootViewController =  tabbarVc;
            
        }];
    }];
}

@end
